﻿using Abp.Threading.BackgroundWorkers;
using Abp.Threading.Timers;
using AbpProjectTemplate.Workers;
using System;
using System.Globalization;
using System.IO;
using System.Linq;

namespace AbpProjectTemplate.Web.Logs
{
    //public class RemoveLogsAppWorker : PeriodicBackgroundWorkerBase, IAppWorker
    //{
    //    public RemoveLogsAppWorker(AbpTimer abpTimer)
    //       : base(abpTimer)
    //    {
    //        this.Timer.RunOnStart = true;
    //        this.Timer.Period = 1000 * 60 * 60 * 24;
    //    }

    //    protected override void DoWork()
    //    {
    //        var logRootPath = Path.Combine(Path.GetDirectoryName(this.GetType().Assembly.Location), "Logs");
    //        if (!Directory.Exists(logRootPath))
    //            return;
    //        var folders = Directory.GetDirectories(logRootPath);
    //        foreach (var folder in folders)
    //        {
    //            string[] arr = folder.Split('\\');
    //            if (arr.Length <= 1)
    //                arr = folder.Split('/');
    //            var name = arr.Last();
    //            if (DateTime.Parse(name) <= DateTime.Now.Date.AddDays(-15))
    //                Directory.Delete(folder, true);
    //        }
    //        var files = Directory.GetFiles(logRootPath);
    //        foreach (var file in files)
    //        {
    //            var name = Path.GetFileNameWithoutExtension(file);
    //            if (DateTime.TryParseExact(name, "yyyyMMdd", null, DateTimeStyles.None, out DateTime date))
    //            {
    //                if (date <= DateTime.Now.Date.AddDays(-15))
    //                    File.Delete(file);
    //            }
    //        }
    //    }
    //}
}
